import random

def enbase(text, alphabet):
    return ''.join([alphabet[ord(char) >> 4] + alphabet[23 - (ord(char) & 0x0F)] for char in text])

def debase(text, alphabet):
    if not len(text) % 2 == 0:
        raise ValueError('Length of input should be multiple of 2')

    return ''.join([chr(((alphabet.index(text[i << 1]) << 4) | (23 - alphabet.index(text[(i << 1) + 1])))) for i in xrange(len(text) >> 1)])

def add_space(text):
    if len(text) < 24:
        buf = list(text)
        [buf.insert(random.randint(1, len(text)), ' ') for i in xrange(random.randint(0, int(len(text)/6)))]
    else:
        buf = []
        pos = 0
        text = buffer(text)
        while pos < len(text):
            if len(text) - pos < 8:
                buf.append(text[pos:])
                break
            if len(text) - pos < 12:
                i = random.randint(2, 7)
                buf.append(text[pos:pos+i])
                pos = pos + i
                buf.append(' ')
                buf.append(text[pos:])
                break
            i = random.randint(1, int((len(text)-pos)/12))
            buf.append(text[pos:pos+i])
            pos = pos + i
            if random.randint(0, 20) < 8:
                buf.append(' ')

    return ''.join(buf)

def url_add_slashes_underline(text):
    buf = list(text)
    [buf.insert(random.randint(1, len(text)), '/') for i in xrange(random.randint(0, int(len(text)/6)))]
    [buf.insert(random.randint(1, len(text)), '_') for i in xrange(random.randint(0, int(len(text)/8)))]
    return ''.join(buf)

def create_random_string(maxlen, alphabet):
    buf = []
    for i in xrange(random.randint(0, maxlen)):
        if random.uniform(0, 2) > 1.4:
            buf.append(random.choice(alphabet).upper())
        else:
            buf.append(random.choice(alphabet).lower())
    return ''.join(buf)